package com.yuyang.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyang.common.constant.ProductConstants;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.Query;
import com.yuyang.gulimall.product.dao.AttrDao;
import com.yuyang.gulimall.product.dao.AttrGroupDao;
import com.yuyang.gulimall.product.dao.CategoryDao;
import com.yuyang.gulimall.product.entity.*;
import com.yuyang.gulimall.product.service.*;
import com.yuyang.gulimall.product.vo.AttrRespVo;
import com.yuyang.gulimall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.save(attrEntity);
        if (ProductConstants.AttrType.ATTR_TYPE_BASE.getCode().equals(attr.getAttrType())&& attr.getAttrGroupId() != null){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }
    }



    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId, String attrType) {
        Integer attrTypeInt = (attrType.equalsIgnoreCase(ProductConstants.AttrType.ATTR_TYPE_BASE.getMsg()))?1:0;
        LambdaQueryWrapper<AttrEntity> lambdaQueryWrapper = new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getAttrType,attrTypeInt);
        String key = (String) params.get("key");
        lambdaQueryWrapper.eq(catelogId != 0, AttrEntity::getCatelogId, catelogId).and(StringUtils.isNotBlank(key), (wrapper) -> {
                    wrapper.eq(AttrEntity::getAttrId, key).or().
                            like(AttrEntity::getAttrName, key);
                }
        );
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                lambdaQueryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> attrEntityList = page.getRecords();
        //设置所属的属性分组和分类
        List<AttrRespVo> attrRespVoList = attrEntityList.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
            //如果是销售属性设置属性分组
            if (attrTypeInt.equals(ProductConstants.AttrType.ATTR_TYPE_BASE.getCode())){
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().
                        eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null && attrAttrgroupRelationEntity.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                    if (attrGroupEntity != null) {
                        attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                }
            }

//            设置所属的分类
            CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());

        pageUtils.setList(attrRespVoList);
        return pageUtils;
    }

    @Transactional
    @Override
    public AttrVo getInfo(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrVo attrVo = new AttrVo();
        BeanUtils.copyProperties(attrEntity, attrVo);
        //设置关联的分类路径
        Long catelogId = attrEntity.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrVo.setCatelogPath(catelogPath);
        //设置关联属性分组的id
        if (ProductConstants.AttrType.ATTR_TYPE_BASE.getCode().equals(attrEntity.getAttrType())){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().
                    eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
            if (attrAttrgroupRelationEntity != null) {
                attrVo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
            }
        }

        return attrVo;
    }

    @Transactional
    @Override
    public void updateAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.updateById(attr);
        //如果是销售属性更新分组
        if (ProductConstants.AttrType.ATTR_TYPE_BASE.getCode().equals(attrEntity.getAttrType())) {
            Long attrId = attrEntity.getAttrId();

            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attrId);

            long count = attrAttrgroupRelationService.count(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().
                    eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
            if (count >0 ){
                attrAttrgroupRelationService.update(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().
                        eq(AttrAttrgroupRelationEntity::getAttrId, attrId).set(AttrAttrgroupRelationEntity::getAttrGroupId, attrAttrgroupRelationEntity.getAttrGroupId()));
            }else {
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }


        }
    }

    @Override
    public List<ProductAttrValueEntity> baseAttrBySpuId(Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueService.list(new LambdaUpdateWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId, spuId));
        return list;
    }
    @Transactional
    @Override
    public void updateBySpuId(Long spuId, List<ProductAttrValueEntity> productAttrValueEntityList ) {
        productAttrValueService.remove(new LambdaUpdateWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId,spuId));
        List<ProductAttrValueEntity> collect = productAttrValueEntityList.stream().peek(productAttrValue -> productAttrValue.setSpuId(spuId)).collect(Collectors.toList());
        productAttrValueService.saveBatch(collect);
    }

    @Override
    public List<Long> getSearchTypeAttrIds(List<Long> attrIds) {
        LambdaQueryWrapper<AttrEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AttrEntity::getAttrId,attrIds).
                eq(AttrEntity::getSearchType,ProductConstants.AttrSearchTypeEnum.NO_NEED_SEARCH.getCode());

        return this.list(lambdaQueryWrapper).stream().map(AttrEntity::getAttrId).collect(Collectors.toList());
    }
}